package com.dmyang.controller;

import com.dmyang.dao.JedisClientSingle;
import com.dmyang.vo.UserVo;
import com.dmyang.websocket.MyWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.socket.WebSocketSession;

import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by brss on 2018/5/12.
 */
@Controller
@RequestMapping("/patient")
public class PatientController {

    @Autowired
    private JedisClientSingle jedisClientSingle;

    @RequestMapping("/initPat")
    public String initPat(){
        return "patInit";
    }

    @RequestMapping("/patInitAddSession")
    public String addSession(HttpSession session, @RequestParam("name") String name,
                             @RequestParam String primaryKey,
                             @RequestParam String role){
        session.setAttribute("pk",primaryKey);
        session.setAttribute("name",name);
        session.setAttribute("role",role);
        return "DPChat_patient";
    }


    @RequestMapping("/DPChat")
    public String DPChat(){

        return "DPChat_patient";
    }

    /**
     * 病人调用的接口
     * @param session
     * @param reqPk 请求用户的主键
     * @return 获取空闲医生（有多个的话，只返回一个）
     */

    @RequestMapping("/freeDocList")
    @ResponseBody
    public synchronized List<UserVo> onlineusers(HttpSession session, @RequestParam String reqPk){
        Map<String, WebSocketSession> map= MyWebSocketHandler.userSocketSessionMap;
        Map<String, List<UserVo>> relation_doc = MyWebSocketHandler.relation_doc;
        Map<String, List<UserVo>> relation_pat = MyWebSocketHandler.relation_pat;

        return relation_pat.get(reqPk);
    }


    /*
    @RequestMapping("/freeDocList")
    @ResponseBody
    public synchronized List<UserVo> onlineusers(HttpSession session, @RequestParam String reqPk){
        Map<String, WebSocketSession> map= MyWebSocketHandler.userSocketSessionMap;
        Set<String> set=map.keySet();
        Iterator<String> it = set.iterator();
        Map<String,String> onlineMap = new ConcurrentHashMap<>();
        //List<UserVo> onlineList = new ArrayList();
        //获取chatWith
        List chatWithList = (List) map.get(reqPk).getAttributes().get("chatWith");
        //如果可以从容器中取到值，则证明两人已经建立聊天
        if (chatWithList.size()!=0){
            return chatWithList;
        }
        while(it.hasNext()){
            //当前请求用户的pk
            //String nowUserPk = (String) session.getAttribute("pk");
            String pk = it.next();
            String name = (String) map.get(pk).getAttributes().get("name");
            String role = (String) map.get(pk).getAttributes().get("role");

            boolean isBusy=true;
            if (role.equals("doctor")){
                isBusy = (boolean)map.get(pk).getAttributes().get("isBusy");
            }
            //把当前访问的人从在线列表中去除 ,把医生剔除,把已经被接诊的患者删除
            if (!pk.equals(reqPk) && role.equals("doctor") && !isBusy)
                onlineMap.put(pk,name);
        }
        Set<String> keyset = onlineMap.keySet();
        for (String key : keyset){
            UserVo userVo = new UserVo();
            userVo.setId(key);
            userVo.setName(onlineMap.get(key));
            userVo.setType("kefu");
            userVo.setAvatar("//res.layui.com/images/fly/avatar/00.jpg");
            //onlineList.add(userVo);
//          把医生状态修改为繁忙
            //TODO
            int treatNum = (int) map.get(key).getAttributes().get("treatNum");
            if (treatNum >=3 ){
                map.get(key).getAttributes().put("isBusy",true);
            }else {
                treatNum++;
                map.get(key).getAttributes().put("treatNum",treatNum);
                chatWithList.add(userVo);
                break;
            }
        }
        map.get(reqPk).getAttributes().put("chatWith",chatWithList);
        //把病人状态修改为已经被接诊
        map.get(reqPk).getAttributes().put("isTreated",true);
        return chatWithList;
    }*/


}
